import pandas as pd
import seaborn as sns
import plotly.express as px
import matplotlib.pyplot as plt
import plotly.io as pio
pio.renderers.default = "plotly_mimetype+notebook"
For this excercise, we have written the following code to load the stock dataset built into plotly express.
stocks = px.data.stocks()
stocks.head()
Select a stock and create a suitable plot for it. Make sure the plot is readable with relevant information, such as date, values.
stocks.plot(x = 'date', y = 'NFLX', figsize = (12, 8))
plt.ylabel('stock value')
plt.title('Stock value Netflix')
plt.legend('',frameon=False);
You've already plot data from one stock. It is possible to plot multiples of them to support comparison.
To highlight different lines, customise line styles, markers, colors and include a legend to the plot.
stocks.plot(x ='date', figsize = (12, 8), style = ['-', '.', '-.', 'o', '--', ':'])
plt.ylabel('stock value')
plt.title('Stock values');
First, load the tips dataset
tips = sns.load_dataset('tips')
tips.head()
Let's explore this dataset. Pose a question and create a plot that support drawing answers for your question.
Some possible questions:
fig = sns.barplot(x = 'day', y = 'tip', hue = 'sex', data = tips)
fig.set(ylabel = 'avg of tip')
plt.legend(bbox_to_anchor=(1.1, 1), loc=2, borderaxespad=0.)
plt.title('What is the average tip given by men and women per day of the week?');
Redo the above exercises (challenges 2 & 3) with plotly express. Create diagrams which you can interact with.
Hints:
stocks = px.data.stocks()
fig = px.line(stocks, x='date', y=stocks.columns[1:], title='Stock values')
fig.update_layout(yaxis_title = 'stock value', legend_title = '')
fig.show()
tips = px.data.tips()
fig = px.histogram(tips, x = 'day', y = 'tip',
color='sex', barmode='group',
histfunc='avg', category_orders={'day': ['Thur', 'Fri', 'Sat', 'Sun'], 'sex' : ['Male', 'Female']})
fig.show()
Recreate the barplot below that shows the population of different continents for the year 2007.
Hints:
#load data
df = px.data.gapminder()
df.head()
df = df.drop(df[df['year'] != 2007].index)
fig = px.histogram(df,
x = 'pop',
y = 'continent',
color = 'continent',
category_orders={"continent": ["Asia", "Africa", "Americas", "Europe", "Oceania"]},
text_auto = '.2s'
)
fig.update_traces(textposition = 'outside')
fig.update_layout(xaxis_title = 'pop', showlegend = False)
fig.show()